﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Transport;

namespace Mimoza.DBLayer.Transport.Server
{
/*    public class SubsriptionManager :
        ISubsriptionRequestProcessor
    {
        public SubsriptionManager()
        {
            SubsriptionRequest.RegisterHandler(ProcessRequest);
        }

        public void AddSubsription(ISubsription Subsription, Connection conn)
        {
            lock (m_Subsriptions)
            {
                m_Subsriptions[Subsription.SubsriptionID] = new SubsriptionItem(Subsription, conn);
                conn.CloseHandler += OnConnectionClose;
            }
        }

        public void DeleteSubsription(Guid SubsriptionId)
        {
            lock (m_Subsriptions)
            {
                if (m_Subsriptions.ContainsKey(SubsriptionId))
                {
                    m_Subsriptions.Remove(SubsriptionId);

                }
            }
        }

        void OnConnectionClose(Connection conn)
        {
            ISubsription foundConn = null;
            lock (m_Subsriptions)
            {
                foreach (KeyValuePair<Guid, SubsriptionItem> pair in m_Subsriptions)
                {
                    if (pair.Value.Connection == conn)
                    {
                        foundConn = pair.Value.Subsription;
                        DeleteSubsription(pair.Key);
                        break;
                    }
                }
            }

            if (foundConn != null)
            {
                foundConn.Dispose();
            }
        }

        SubsriptionItem FindSubsription(Guid SubsriptionId)
        {
            SubsriptionItem result = null;
            lock (m_Subsriptions)
            {
                if (!m_Subsriptions.ContainsKey(SubsriptionId))
                {
                    throw new ArgumentException();
                }

                result = m_Subsriptions[SubsriptionId];
            }
            return result;
        }

        public TransferObject ProcessRequest(SubsriptionRequest request)
        {
            SubsriptionItem SubsriptionItem = FindSubsription(request.SubsriptionTransfer.ID);

            TransferObject result = null;
            switch (request.SubsriptionTransfer.OperationType)
            {
                case SubsriptionTransfer.Operation.Reset:
                    result = SubsriptionItem.Subsription.Reset();
                    break;
                case SubsriptionTransfer.Operation.MoveNext:
                    result = SubsriptionItem.Subsription.MoveNext();
                    break;
                case SubsriptionTransfer.Operation.Current:
                    result = SubsriptionItem.Subsription.Current;
                    break;
                case SubsriptionTransfer.Operation.Dispose:
                    result = SubsriptionItem.Subsription.Dispose();
                    DeleteSubsription(request.SubsriptionTransfer.ID);
                    break;
            }

            return result;
        }


        class SubsriptionItem
        {
            public SubsriptionItem()
            {
            }
            public SubsriptionItem(ISubsription Subsription, Connection connection)
            {
                Subsription = Subsription;
                Connection = connection;
            }

            public ISubsription Subsription;
            public Connection Connection;
        };

        Dictionary<Guid, SubsriptionItem> m_Subsriptions = new Dictionary<Guid, SubsriptionItem>();
    }*/
}
